<?php
class Codes_model extends CI_Model {

	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

	function add_new_code_type()
	{
		/*
		 *  save the code type added by user to database
		 *  
		 */ 
		date_default_timezone_set('America/New_York');
		$created_date = date("Y-m-d");
		$data = array(
					'code_type_name' => $this->input->post('code_type_name'),
					'code_type_description' => $this->input->post('code_type_description'),
					
					'created_date' => $created_date,
					'updated_date' => $created_date,
					'created_by' => $this->session->userdata('username'),
					'updated_by' => $this->session->userdata('username'),
					'deprecated' => 'N',
				);
		
		$this->db->insert('vm_code_types',$data);

	}
	
	function view_all_code_types(){
		/*
		 *  construct a list of code types data and send back to controller
		 * */
		$this->db->select('code_type_id, code_type_name,code_type_description, created_date, created_by, deprecated')->from('vm_code_types');

		$results_array = $this->db->get();
		
		return $results_array;
		
	}
	
	function view_all_code_systems(){
		/*
		 *  construct a list of code systems data and send back to controller
		 * */
		
		$this->db->select('vm_code_systems.coding_system_id, vm_code_systems.coding_system_name, vm_code_systems.abbreviation, 
			vm_code_types.code_type_name, vm_code_systems.created_date, vm_code_systems.created_by, vm_code_systems.deprecated')
				->from('vm_code_systems')->join('vm_code_types', 'vm_code_systems.code_type_id = vm_code_types.code_type_id');

		$results_array = $this->db->get();
		
		return $results_array;
	}
	
	function add_new_code_system(){
		
		/*
		 *  save the code system added by user to database
		 *  
		 */ 
		date_default_timezone_set('America/New_York');
		$created_date = date("Y-m-d");
		$data = array(
					'coding_system_name' => $this->input->post('code_system_name'),
					'abbreviation' => $this->input->post('abbreviation'),
					'code_type_id' => $this->input->post('code_type_id'),
					
					'created_date' => $created_date,
					'updated_date' => $created_date,
					'created_by' => $this->session->userdata('username'),
					'updated_by' => $this->session->userdata('username'),
					'deprecated' => 'N',
				);
		
		$this->db->insert('vm_code_systems',$data);
	}
	
	// code components start
	function view_all_code_components(){
		/*
		 *  construct a list of code components data and send back to controller
		 * */
		
		$this->db->select('vm_code_components.code_system_id, vm_code_systems.coding_system_name, vm_code_components.code_component_id, 
		vm_code_components.component_name, vm_code_components.component_description, vm_code_components.created_date, vm_code_components.created_by, 
		vm_code_components.deprecated')
				->from('vm_code_components')->join('vm_code_systems', 'vm_code_systems.coding_system_id = vm_code_components.code_system_id');

		$results_array = $this->db->get();
		
		return $results_array;
	}
	
	function add_new_code_component(){
		
		/*
		 *  save the code component added by user to database
		 *  
		 */ 
		date_default_timezone_set('America/New_York');
		$created_date = date("Y-m-d");
		$data = array(
					'component_name' => $this->input->post('component_name'),
					'component_description' => $this->input->post('component_description'),
					'code_system_id' => $this->input->post('code_system_id'),
					
					'created_date' => $created_date,
					'updated_date' => $created_date,
					'created_by' => $this->session->userdata('username'),
					'updated_by' => $this->session->userdata('username'),
					'deprecated' => 'N',
				);
		
		$this->db->insert('vm_code_components',$data);
	}
	// code components end	
}
?>